package com.zr.userrepay.service.impl;

import com.zr.userrepay.mapper.UserRepayMapper;
import com.zr.userrepay.pojo.*;
import com.zr.userrepay.service.UserRepayService;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import com.zr.util.ResultVOBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by star on 2019/7/3.
 */
@Service
public class UserRepayServiceImpl implements UserRepayService{

    @Autowired
    private UserRepayMapper userRepayMapper;


    @Override
    public ResultVo addUserRepay(UserRepayAddVo userRepayAddVo,HttpServletRequest request) {
        //给数据库中的创建时间和修改时间等信息赋值
        UserRepay userRepay = userRepayMapper.findByLoginId(userRepayAddVo.getLoginId());
        UserLoginVo userLoginVo= (UserLoginVo) request.getSession().getAttribute("loginId");
        userRepayAddVo.setLoginId(userLoginVo.getId());
        BeanUtils.copyProperties(userRepayAddVo,userRepay);
        if(userRepay!=null){
            userRepay.setUpdateName(userLoginVo.getUpdateName());
            userRepay.setUpdateTime(new Date());
            userRepayMapper.updateUserRelation(userRepay);
        }else {
            userRepay.setCreateTime(new Date());
            userRepay.setCreateName(userLoginVo.getCreateName());
            userRepay.setUpdateName(userLoginVo.getUpdateName());
            userRepay.setUpdateTime(new Date());
            userRepayMapper.addUserRelation(userRepay);
        }
        return ResultBuildVo.success(userRepayAddVo);
    }

    @Override
    public ResultVo findByLoginId(Integer loginId) {
        if (loginId==null) {
            return ResultBuildVo.error("注册信息表ID不能为空","500");
        }
        UserRepay userRepay = userRepayMapper.findByLoginId(loginId);
        return ResultBuildVo.success(userRepay);
    }








}
